import { stringify } from 'qs';
import { omitBy, isUndefined } from 'lodash';
import request from '@/utils/request';

export const fetchData = async (params: {
  name?: string;
  email?: string;
  pageNo: number;
  pageSize: number;
}) => {
  const result = await request(
    `/msdrivingmonitor/module/page?${stringify(omitBy(params, isUndefined))}`,
  );

  if (result.statusCode !== '0') return {};

  return result.data;
};

export const addModule = async (params: { name: string; description?: string; email?: string }) =>
  await request(`/msdrivingmonitor/module/add`, {
    method: 'POST',
    data: omitBy(params, isUndefined),
  });

export const editModule = async (params: {
  uuid: string;
  name: string;
  description?: string;
  email?: string;
}) =>
  await request(`/msdrivingmonitor/module/edit`, {
    method: 'PUT',
    data: omitBy(params, isUndefined),
  });

export const deleteModule = async (uuid: string) => {
  await request(`/msdrivingmonitor/module/delete?uuid=${uuid}`, {
    method: 'DELETE',
  });
};
